package com.wl.api.service.business.impl;

import static com.wl.api.common.utils.PageInfo.transformPage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wl.api.common.utils.CollectionUtil;
import com.wl.api.common.utils.PageInfo;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.dto.business.CustomerFundDetailDTO;
import com.wl.api.mapper.business.CustomerFundDetailMapper;
import com.wl.api.model.business.CustomerFundDetail;
import com.wl.api.model.business.CustomerUser;
import com.wl.api.service.business.ICustomerFundDetailService;
import com.wl.api.service.business.ICustomerUserService;


/**
 *
 * CustomerFundDetail 表数据服务层接口实现类
 *
 */
@Service
public class CustomerFundDetailServiceImpl extends ServiceImpl<CustomerFundDetailMapper, CustomerFundDetail> implements ICustomerFundDetailService {
		
	@Autowired
	ICustomerUserService customerUserService;

	public PageInfo<CustomerFundDetailDTO> getItemList(CustomerFundDetailDTO param) {
		Wrapper<CustomerFundDetail> wrapper = new EntityWrapper<CustomerFundDetail>();
		wrapper.where("1=1");
		if (param.getCustId() != null) {
			wrapper.andNewSimple("cust_id", param.getCustId());
		}
		if (param.getStatus() != null) {
			wrapper.andNew("status={0}", param.getStatus());
		} 
		if (!StringUtil.isEmpty(param.getStartTime())) {
			wrapper.gt("create_time", param.getStartTime());
		}
		if (!StringUtil.isEmpty(param.getEndTime())) {
			wrapper.lt("create_time", param.getStartTime());
		}
		if (!StringUtil.isEmpty(param.getUsername())) {
			List<CustomerUser> userList = customerUserService.selectList(new EntityWrapper<CustomerUser>().where("username",  param.getUsername()));
			wrapper.in("cust_id", CollectionUtil.isEmpty(userList) ? Arrays.asList(0) : userList.parallelStream().map(CustomerUser::getCustId).collect(Collectors.toList()));
		}
		
		Page<CustomerFundDetail> page = selectPage(new Page<>(param.getPage(), param.getPageSize(), "create_time" ,false), wrapper); 
		PageInfo<CustomerFundDetailDTO> pageInfo = transformPage(CustomerFundDetailDTO.class, page);
		if (!CollectionUtil.isEmpty(pageInfo.getItems())) {
			
			List<CustomerUser> userList = customerUserService.selectBatchIds(pageInfo.getItems().parallelStream().map(CustomerFundDetailDTO::getCustId).collect(Collectors.toList()));
			Map<Long, String> userMap = userList.parallelStream().collect(Collectors.toMap(CustomerUser::getCustId, CustomerUser::getUsername));
			
			for(CustomerFundDetailDTO dd: pageInfo.getItems())  {
				dd.setStLabel(dd.getStatus() == 0 ? "confirming(确认中)": (dd.getStatus() ==1? "confirmed(已确认)": "failure(失败)"));
				dd.setIsEdit(dd.getStatus() == 1 ? 0: 1);
				dd.setUsername(userMap.get(dd.getCustId()));
			}
				
		}
		return pageInfo;
	}
	
}